﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.BilirubinMonitoring
{
    public partial class AddBilirubinMonitoring : Form
    {
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        string Id;
        public AddBilirubinMonitoring(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            this.Id = id;
            BindInfo(id);
        }
        public AddBilirubinMonitoring(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            BindInfo(this.Id);
        }
        public AddBilirubinMonitoring(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.PatientInfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            BindInfo(this.Id);
        }

        #region 保存
        private void simplebaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDate.Text))
                txtDate.DateTime = DateTime.Now;

            Bilirubin dhs = new Bilirubin();
            dhs.FormId = (int)FormType.Bilirubin;
            dhs.FormName = EnumHelper.GetDescription(FormType.Bilirubin);
            dhs.JianCeRiQi = txtDate.Text;
            dhs.ShiJian = DateTimeTran.TimeFillZero(txtTime.Text);
            dhs.DanHongSu = txtDanhongsu.Text;

            record.NursingFormData = XmlUtil.ObjectToXML(dhs, Encoding.UTF8);
            record.FormId = dhs.FormId;//表单ID
            record.FormName = dhs.FormName;//表单NAME
            record.PatientId = PatientInfo.PATIENT_ID;//住院号
            record.VisitId = PatientInfo.VISIT_ID;//住院次数
            record.PatientName = PatientInfo.PATIENT_NAME;//患者名称
            record.BedNo = PatientInfo.BED_NO;//床号
            record.DepartmentId = PatientInfo.DEPARTMENT_ID;//科室号
            record.DepartmentName = PatientInfo.DEPARTMENT_NAME;//科室名称
            record.AreaId = PatientInfo.NURSE_CELL_CODE;//病区编号
            record.AreaName = PatientInfo.NURSE_CELL_NAME;//病区名称
            record.AssessmentDate = Convert.ToDateTime(txtDate.Text);//日期       

            record.Id = string.IsNullOrEmpty(Id) ? 0 : Convert.ToInt32(Id);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));

            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 取消
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void BindInfo(string id)
        {
            txtDate.DateTime = DateTime.Now;
            txtTime.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Bilirubin ps = new Bilirubin();
            ps = XmlUtil.XMLToObject<Bilirubin>(NursingFormData, Encoding.UTF8);
            txtDate.Text = ps.JianCeRiQi;
            txtTime.Text = DateTimeHelper.HHmmssToHHmm(ps.ShiJian);
            txtDanhongsu.Text = ps.DanHongSu;
        }
    }
}
